package com.budwk.app.sqfwV2.service.baiyunqu;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.SqfwFormSummaryConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.DownDataVo;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.config.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.excelService.CompleteFlowExcelService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowExcelParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelGenerator;
import com.budwk.app.sqfwV2.dto.baiyunqu.OldPeopleSeekPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.enums.OldPeopleSeekEnum;
import com.budwk.app.sqfwV2.generator.baiyunqv.OldPeopleSeekExcelGenerator;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.security.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-06-16
 *
 * 白云区特殊困难老年人探访 OldPeopleSeek
 */
@IocBean
public class OldPeopleSeekExcelService extends CompleteFlowExcelService {
    @Inject
    SqfwFormsummaryNewService sqfwFormsummaryNewService;
    @Inject
    SysUserService sysUserService;

    /**
     * app端下载 2.0版本
     * @param reqVo
     * @return
     */
    public DownDataVo<ResultOfExcelGenerator> createExcel(DownExcelLedgerReqVo reqVo) {
        List<ExcelCreateConfig> excelConfigList = new ArrayList<>();
        if (reqVo.isDownTemplate()) {
            ExcelCreateConfig config = new ExcelCreateConfig();
            config.setFileIdentity(reqVo.getFileCode());
            config.setOnlyGenerateTemplateFlag(true);
            excelConfigList.add(config);
        } else {
            Map<String,Object> param = new HashMap<>();
            param.put("start", reqVo.getFillStartDate());
            param.put("end", reqVo.getFillEndDate());
            param.put("personInfoId", reqVo.getQueryKey());
            if (reqVo.isCheckData()) {
                for (OldPeopleSeekEnum excel : OldPeopleSeekEnum.values()) {
                    ExcelCreateConfig config = new ExcelCreateConfig();
                    config.setFileIdentity(excel.getCode());
                    config.setCreateExcelFileFlag(false);
                    config.setParam(param);
                    excelConfigList.add(config);
                }
            } else {
                ExcelCreateConfig config = new ExcelCreateConfig();
                config.setFileIdentity(reqVo.getFileCode());
                config.setCreateExcelFileFlag(true);
                config.setParam(param);
                excelConfigList.add(config);
            }
        }

        ExcelCreateConfig config = excelConfigList.get(0);

        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary_new::getCreateBy, "=", SecurityUtil.getUserId());
        cnd.and(Sqfw_formSummary_new::getType, "=", SqfwFormSummaryConstants.old_people_seek);
        if ((config.getParam().get("start") != null && StringUtils.isNotBlank(config.getParam().get("start").toString()))) {
            cnd.and(Sqfw_formSummary_new::getFillDate, ">=", config.getParam().get("start"));
        }
        if ((config.getParam().get("end") != null && StringUtils.isNotBlank(config.getParam().get("end").toString()))) {
            cnd.and(Sqfw_formSummary_new::getFillDate, "<=", config.getParam().get("end") + " 23:59:59");
        }
        if ((config.getParam().get("personInfoId") != null && StringUtils.isNotBlank(config.getParam().get("personInfoId").toString()))) {
            cnd.and("param_data->>'id'", "=", config.getParam().get("personInfoId"));
        }

        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query("id", cnd);

        List<String> ids = formSummaryNewList.stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList());

        CompleteFlowExcelParamForController paramForController = new CompleteFlowExcelParamForController();
        paramForController.setExcelCreateConfigList(excelConfigList);
        paramForController.setFormSummaryIdList(ids);
        paramForController.setUserId(SecurityUtil.getUserId());
        List<ResultOfExcelGenerator> result = super.createExcelForAdmin(paramForController);
        return new DownDataVo<>(formSummaryNewList.size(), result);
    }

    /**
     * app端下载
     * @param excelConfigList
     * @param sqfwIdList
     * @return
     */
    public List<ResultOfExcelGenerator> createExcel(List<ExcelCreateConfig> excelConfigList, List<String> sqfwIdList) {
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary_new::getSqfwId, "in", sqfwIdList);
        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query("id", cnd);

        List<String> ids = formSummaryNewList.stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList());

        CompleteFlowExcelParamForController paramForController = new CompleteFlowExcelParamForController();
        paramForController.setExcelCreateConfigList(excelConfigList);
        paramForController.setFormSummaryIdList(ids);
        paramForController.setUserId(SecurityUtil.getUserId());
        List<ResultOfExcelGenerator> result = super.createExcelForAdmin(paramForController);
        return result;
    }

    /**
     * 后台下载
     * @param excelConfigList
     * @param summaryIdList
     * @return
     */
    public List<ResultOfExcelGenerator> createExcelForAdmin(List<ExcelCreateConfig> excelConfigList, List<String> summaryIdList) {
        CompleteFlowExcelParamForController paramForController = new CompleteFlowExcelParamForController();
        paramForController.setExcelCreateConfigList(excelConfigList);
        paramForController.setFormSummaryIdList(summaryIdList);
        paramForController.setUserId(SecurityUtil.getUserId());
        List<ResultOfExcelGenerator> result =  super.createExcelForAdmin(paramForController);
        return result;
    }

    /**
     * 获取单位所有excel
     * @param excelConfigList
     * @return
     */
    public List<ResultOfExcelGenerator> createExcelToUnit(List<ExcelCreateConfig> excelConfigList) {
        Sys_user user = sysUserService.getUserById(SecurityUtil.getUserId());
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary_new::getUnitId, "=", user.getUnitId());
        cnd.and(Sqfw_formSummary_new::getType, "=", SqfwFormSummaryConstants.old_people_seek);
        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query("id", cnd);

        List<String> ids = formSummaryNewList.stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList());

        CompleteFlowExcelParamForController paramForController = new CompleteFlowExcelParamForController();
        paramForController.setExcelCreateConfigList(excelConfigList);
        paramForController.setFormSummaryIdList(ids);
        paramForController.setUserId(SecurityUtil.getUserId());
        List<ResultOfExcelGenerator> result = super.createExcelForAdmin(paramForController);
        return result;
    }

    @Override
    protected void customerPopularExcelGenerateParam(ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    public List<ExcelGeneratorConfig> getExcelGeneratorConfig(List<ExcelCreateConfig> excelCreateConfigList) {
        List<ExcelGeneratorConfig> generatorConfigList = new ArrayList<>();

        for (ExcelCreateConfig excelCreateConfig : excelCreateConfigList) {
            if (OldPeopleSeekEnum.CARE_SERVICE_RECORD_EXCEL.getCode().equals(excelCreateConfig.getFileIdentity())) {
                ExcelGeneratorConfig excelGeneratorConfig = new ExcelGeneratorConfig();
                excelGeneratorConfig.setExcelGenerator(new OldPeopleSeekExcelGenerator());
                excelGeneratorConfig.setExcelCreateConfig(excelCreateConfig);
                generatorConfigList.add(excelGeneratorConfig);
            }
        }
        return generatorConfigList;
    }

    @Override
    public AbstractCustomerPdfCreateField getAbstractCustomerField(String jsonStr) {
        return JSONObject.parseObject(jsonStr, OldPeopleSeekPdfDTO.class);
    }
}
